package com.lotto.manager.admin.service.impl;

import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import com.lotto.manager.admin.dao.KitheMapper;
import com.lotto.manager.admin.entity.db.Kithe;
import com.lotto.manager.admin.service.KitheService;
import com.lotto.manager.common.utils.DateUtil;



public class KitheServiceImpl implements KitheService {
	
	@Autowired
	private KitheMapper kitheMapper;
	@Override
	public Kithe createKithe() {
		Kithe tmekithe = kitheMapper.selectNewest();
		Kithe kithe = new Kithe();
		if(null != tmekithe){
			int addDate = nextDateForAdd(tmekithe);
			kithe.setTmtime(new Date(tmekithe.getTmtime().getTime()+addDate*86400000));
			kithe.setNotmtime(new Date(tmekithe.getNotmtime().getTime()+addDate*86400000));
			kithe.setKialltime(new Date(tmekithe.getKialltime().getTime()+addDate*86400000));
			kithe.setOpalltime(new Date(tmekithe.getOpalltime().getTime()+addDate*86400000));
			kithe.setKitheNb(kitheNumber(tmekithe));
		}
		kitheMapper.insert(kithe);
		return kithe;
	}
	
	


	@Override
	public void updateKithe(Kithe kithe) {
		kitheMapper.updateByPrimaryKeySelective(kithe);
	}


	@Override
	public Kithe findNewtheKithe() {
		Kithe tmekithe = kitheMapper.selectNewest();
		return tmekithe;
	}
	
	
	
	/**============================================下面为局部方法==================================================
	 */
	
	/*
	 * 开奖期号 
	 * 在原来的基础上加1
	 */
	private Integer kitheNumber(Kithe kithe){
		if(null != kithe){
			return kithe.getKitheNb()+1;
		}
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		return Integer.valueOf(year+"999");
	}
	
	/*
	 * 下期开奖时间 需要加多少天
	 * 
	 */
	private int nextDateForAdd(Kithe kithe){
		int week = DateUtil.getWeek(kithe.getLotteryTime());
		switch (week) {
		case 6:
		case 7:
			return 3;
		default:
			return 2;
		}		
	}







}
